package com.hnevc.wechatviewdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    LinearLayout mTabWeixin,mTabLianxinren,mTabFaxian,mTabWode;
    Fragment mWeixinFragment,mLianxinrenFragment,
             mFaxianFragment,mWodeFragment;
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTabWeixin = (LinearLayout) findViewById(R.id.id_weixin_tab);
        mTabLianxinren = (LinearLayout)findViewById(R.id.id_lianxinren_tab);
        mTabFaxian = (LinearLayout)findViewById(R.id.id_faxian_tab);
        mTabWode = (LinearLayout)findViewById(R.id.id_wode_tab);
        viewPager = (ViewPager) findViewById(R.id.viewpager);

        mTabWeixin.setSelected(true);

        mWeixinFragment = new WeixinFragment();
        mLianxinrenFragment = new LianxinrenFragment();
        mFaxianFragment = new FaxianFragment();
        mWodeFragment = new WodeFragment();

        final List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(mWeixinFragment);
        fragmentList.add(mLianxinrenFragment);
        fragmentList.add(mFaxianFragment);
        fragmentList.add(mWodeFragment);

        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewPager.setAdapter(adapter);
        //viewPager.addOnPageChangeListener(new V);


    }

    public void tabClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.id_weixin_tab:
                viewPager.setCurrentItem(0);
                mTabWeixin.setSelected(true);
                mTabLianxinren.setSelected(false);
                mTabFaxian.setSelected(false);
                mTabWode.setSelected(false);
                break;
            case R.id.id_lianxinren_tab:
                viewPager.setCurrentItem(1);
                mTabLianxinren.setSelected(true);
                mTabWeixin.setSelected(false);
                mTabFaxian.setSelected(false);
                mTabWode.setSelected(false);
                break;
            case R.id.id_faxian_tab:
                viewPager.setCurrentItem(2);
                mTabFaxian.setSelected(true);
                mTabWeixin.setSelected(false);
                mTabLianxinren.setSelected(false);
                mTabWode.setSelected(false);
                break;
            case R.id.id_wode_tab:
                viewPager.setCurrentItem(3);
                mTabWode.setSelected(true);
                mTabLianxinren.setSelected(false);
                mTabFaxian.setSelected(false);
                mTabWeixin.setSelected(false);
                break;

        }

    }
}
